package cn.xfdzx.android.apps.shop.presenter;

import java.util.Map;

import cn.xfdzx.android.apps.shop.base.BasePresenter;
import cn.xfdzx.android.apps.shop.bean.BaseObjectBean;
import cn.xfdzx.android.apps.shop.bean.ConfirmGoodBeen;
import cn.xfdzx.android.apps.shop.bean.HomeMarketInitDataBeen;
import cn.xfdzx.android.apps.shop.bean.HomeMarketSelectBeen;
import cn.xfdzx.android.apps.shop.bean.HomeSearchRetailGoodsBean;
import cn.xfdzx.android.apps.shop.bean.HomeSearchXFDGoodBeen;
import cn.xfdzx.android.apps.shop.bean.HotSearchBeen;
import cn.xfdzx.android.apps.shop.bean.HotSearchRetailBean;
import cn.xfdzx.android.apps.shop.bean.SearchBeen;
import cn.xfdzx.android.apps.shop.bean.SearchShopBean;
import cn.xfdzx.android.apps.shop.contract.HomeMarketContract;
import cn.xfdzx.android.apps.shop.contract.HomeSearchContract;
import cn.xfdzx.android.apps.shop.model.HomeMarketModel;
import cn.xfdzx.android.apps.shop.model.HomeSearchModel;
import cn.xfdzx.android.apps.shop.net.RxScheduler;
import io.reactivex.functions.Consumer;

/**
 * @author azheng
 * @date 2018/6/4.
 * Description：
 */
public class HomeSearchPresenter extends BasePresenter<HomeSearchContract.View> implements HomeSearchContract.Presenter {

    private HomeSearchContract.Model model;

    public HomeSearchPresenter() {
        model = new HomeSearchModel();
    }


    @Override
    public void initData(String keyword, String indexStatus,String user_current_lng,String user_current_lat) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }

        model.initData(keyword, indexStatus,  user_current_lng,  user_current_lat).compose(RxScheduler.<SearchBeen>Flo_io_main())
                .as(mView.<SearchBeen>bindAutoDispose()).subscribe(new Consumer<SearchBeen>() {
            @Override
            public void accept(SearchBeen baseObjectBean) throws Exception {

                mView.onInitDataSuccess(baseObjectBean);

            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {

                mView.onError(throwable);

            }
        });
    }

    @Override
    public void initHotSearch() {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.initHotSearch().compose(RxScheduler.<HotSearchBeen>Flo_io_main())
                .as(mView.<HotSearchBeen>bindAutoDispose()).subscribe(new Consumer<HotSearchBeen>() {
            @Override
            public void accept(HotSearchBeen baseObjectBean) throws Exception {

                mView.onHotSearchSuccess(baseObjectBean);
                mView.hideLoading();
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {

                mView.onError(throwable);
                mView.hideLoading();
            }
        });
    }

    @Override
    public void initSearchData(boolean isRefresh,boolean isLoading,String lng,  String lat,String sid, String page, String limit,
                               String index_status, String is_store, String store_id, String keyword,
                               String priceOrder, String salesOrder, String news) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        if (isLoading){
            mView.showLoading();
        }
        model.initSearchData(lng,lat,sid, page, limit, index_status, is_store, store_id, keyword, priceOrder, salesOrder, news).compose(RxScheduler.<HomeSearchXFDGoodBeen>Flo_io_main())
                .as(mView.<HomeSearchXFDGoodBeen>bindAutoDispose()).subscribe(new Consumer<HomeSearchXFDGoodBeen>() {
            @Override
            public void accept(HomeSearchXFDGoodBeen baseObjectBean) throws Exception {
                mView.onInitSearchDataSuccess(baseObjectBean);
                if (isLoading){
                    mView.hideLoading();
                }
                if (isRefresh){
                    mView.finishRefresh();
                }else {
                    mView.finishLoadMore();
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mView.onError(throwable);
                if (isLoading){
                    mView.hideLoading();
                }
                if (isRefresh){
                    mView.finishRefresh();
                }else{
                    mView.finishLoadMore();
                }
            }
        });
    }

    @Override
    public void getSearchShopList(Map<String, Object> map) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        mView.showLoading();
        model.getSearchShopList(map).compose(RxScheduler.<SearchShopBean>Flo_io_main())
                .as(mView.<SearchShopBean>bindAutoDispose()).
                subscribe(new Consumer<SearchShopBean>() {
            @Override
            public void accept(SearchShopBean baseObjectBean) throws Exception {

                mView.onGetSearchShopListSuccess(baseObjectBean);
                mView.hideLoading();
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {

                mView.onError(throwable);
                mView.hideLoading();
            }
        });
    }

    @Override
    public void getSearchGoodsList(boolean isRefresh,boolean isLoading,Map<String, Object> map) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        if (isLoading){
            mView.showLoading();
        }
        model.getSearchGoodsList(map).compose(RxScheduler.<HomeSearchRetailGoodsBean>Flo_io_main())
                .as(mView.<HomeSearchRetailGoodsBean>bindAutoDispose()).
                subscribe(new Consumer<HomeSearchRetailGoodsBean>() {
                    @Override
                    public void accept(HomeSearchRetailGoodsBean baseObjectBean) throws Exception {

                        mView.onGetSearchGoodsListSuccess(baseObjectBean);
                        if (isLoading){
                            mView.hideLoading();
                        }
                        if (isRefresh){
                            mView.finishRefresh();
                        }else {
                            mView.finishLoadMore();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                        mView.onError(throwable);
                        if (isLoading){
                            mView.hideLoading();
                        }
                        if (isRefresh){
                            mView.finishRefresh();
                        }else{
                            mView.finishLoadMore();
                        }
                    }
                });
    }

    @Override
    public void addCar(Map<String,Object> map) {
        if (!isViewAttached()) {
            return;
        }
//        mView.showLoading();
        model.addCar(map) .compose(RxScheduler.<ConfirmGoodBeen>Flo_io_main())
                .as(mView.<ConfirmGoodBeen>bindAutoDispose())
                .subscribe(new Consumer<ConfirmGoodBeen>() {
                    @Override
                    public void accept(ConfirmGoodBeen bean) throws Exception {
                        mView.onAddCarSuccess(bean);
//                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
//                        mView.hideLoading();
                    }
                });
    }

    @Override
    public void getRetailHotSearch() {
        if (!isViewAttached()) {
            return;
        }
//        mView.showLoading();
        model.getRetailHotSearch() .compose(RxScheduler.<HotSearchRetailBean>Flo_io_main())
                .as(mView.<HotSearchRetailBean>bindAutoDispose())
                .subscribe(new Consumer<HotSearchRetailBean>() {
                    @Override
                    public void accept(HotSearchRetailBean bean) throws Exception {
                        mView.onGetRetailHotSearchSuccess(bean);
//                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
//                        mView.hideLoading();
                    }
                });
    }

}
